package com.tensquare.qa.controller;

import com.tensquare.qa.client.LabelClient;
import com.tensquare.qa.pojo.Problem;
import com.tensquare.qa.service.ProblemService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.hibernate.Transaction;
import org.hibernate.engine.jdbc.Size;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.imageio.stream.IIOByteBuffer;

/**
 * @Author:zimmor
 * @Date: 2021/3/1 14:52
 * @Description 控制层
 * @version: 1.0.0
 */
@RestController
@CrossOrigin
@RequestMapping("/problem")
public class ProblemController {
    @Autowired
    private ProblemService problemService;

    @Autowired
    private LabelClient labelClient;

    /**
     * 通过feign来调用base微服务
     * @param id
     * @return
     */
    @RequestMapping(value = "/label/{id}")
    public Result findlebelById(@PathVariable String id) {
        return labelClient.findLabelById(id);
    }

    /**
     * 获取等待回答问题列表
     * @param label
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/waitlist/{label}/{page}/{size}")
    public Result waitList(@PathVariable String label, @PathVariable int page, @PathVariable int size) {
        Page pageData = problemService.waitList(label, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Problem>(pageData.getTotalElements(),pageData.getContent()));
    }

    /**
     * 获取最热问题列表
     * @param label
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value="/hotlist/{label}/{page}/{size}")
    public Result hotList(@PathVariable String label, @PathVariable int page, @PathVariable int size) {
        Page pageData = problemService.hotList(label, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Problem>(pageData.getTotalElements(), pageData.getContent()));
    }
    /**
     * 根据标签获取最新问题
     * @param label
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/newlist/{label}/{page}/{size}")
    public Result newlist(@PathVariable String label, @PathVariable int page, @PathVariable int size) {
        Page pageData = problemService.newProblem(label, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Problem>(pageData.getTotalElements(),pageData.getContent()));
    }
    /**
     * 分页查询
     * @param problem
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/search/{page}/{size}")
    public Result findSearch(@RequestBody Problem problem, @PathVariable int page, @PathVariable int size) {
        Page pageData = problemService.findSearch(problem, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Problem>(pageData.getTotalElements(), pageData.getContent()));
    }

    /**
     * 条件查询
     * @param problem
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/search")
    public Result findSearch(@RequestBody Problem problem) {
        return new Result(true, StatusCode.OK, "查询成功", problemService.findSearch(problem));
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public Result findById(@PathVariable String id) {
        return new Result(true, StatusCode.OK, "查询成功", problemService.findById(id));
    }

    /**
     * 新增
     * @param problem
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Problem problem) {
        problemService.add(problem);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    /**
     * 修改
     * @param problem
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/{id}")
    public Result update(@RequestBody Problem problem, @PathVariable String id) {
        problem.setId(id);
        problemService.update(problem);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public Result deleteById(@PathVariable String id) {
        problemService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}
